/* eslint-disable */
var { AsyncRouter: Router } = require('express-async-router')
var { Http404, Http401 } = require('./response')
const sender = (req, res, response) => {
    /**
     * ! 不判空 有隐患 判空有问题
     */
    const isAjax = req.xhr;
    if (response.name === 'response') {
        return res.status(response.status).send(response.content);
    } else if (response.name === 'template') {
        return res.render(response.template, response.context);
    } else if (response.name === 'json') {
        return res.json(response.data);
    } else if (response.name === 'redirect') {
        return res.redirect(response.location);
    } else if (response === Http404) {
        if (isAjax) {
            return res.send({ status: false, err_msg: '页面未找到', data: {} });
        } else {
            return res.sendStatus(404);
        }
    } else if (response === Http401) {
        if (isAjax) {
            return res.send({ status: false, err_msg: '无操作权限', data: {} });
        } else {
            return res.sendStatus(401);
        }
    } else {
        return res.send(response);
    }
};

function AsyncRouter(options = {}) {
    if (!options.sender) {
        options.sender = sender;
    }
    return Router(options);
}
module.exports = AsyncRouter